package com.cnblogs.leizia.crm.workbench.service.impl;

import com.cnblogs.leizia.crm.commen.ErrorCode;
import com.cnblogs.leizia.crm.exception.ClueException;
import com.cnblogs.leizia.crm.settings.domain.User;
import com.cnblogs.leizia.crm.utils.SqlSessionUtil;
import com.cnblogs.leizia.crm.workbench.dao.ClueActivityRelationDao;
import com.cnblogs.leizia.crm.workbench.dao.ClueDao;
import com.cnblogs.leizia.crm.workbench.domain.Activity;
import com.cnblogs.leizia.crm.workbench.domain.Clue;
import com.cnblogs.leizia.crm.workbench.service.ClueService;

import java.util.List;
import java.util.Map;

/**
 * @author leizi
 * @create 2021-03-02 22:51
 */
public class ClueServiceImpl implements ClueService {
    /**
     * 获取创建线索所有者列表
     *
     * @return
     * @throws ClueException
     */
    @Override
    public List<User> getUserList() throws ClueException {
        ClueDao clueDao = SqlSessionUtil.getSqlSession().getMapper(ClueDao.class);

        List<User> userList = clueDao.getUserList();

        if (null == userList || userList.size() == 0) {
            throw new ClueException(ErrorCode.CLUE_GET_USER_LIST_ERROR.getMsg());
        }

        return userList;
    }

    /**
     * 保存线索
     *
     * @param clue 线索实体类
     * @return
     */
    @Override
    public boolean saveClue(Clue clue) {
        ClueDao clueDao = SqlSessionUtil.getSqlSession().getMapper(ClueDao.class);

        int success = clueDao.saveClue(clue);
        if (success > 0) {
            return true;
        }

        return false;
    }

    /**
     * 分页获取线索列表
     *
     * @param map
     * @return
     */
    @Override
    public List<Clue> getPageList(Map<String, Object> map) {
        ClueDao clueDao = SqlSessionUtil.getSqlSession().getMapper(ClueDao.class);
        List<Clue> pageList = clueDao.getPageList(map);

        return pageList;
    }

    /**
     * 跳转线索详情页
     *
     * @param clueId
     * @return
     */
    @Override
    public Clue dispatcherDetail(String clueId) {
        ClueDao clueDao = SqlSessionUtil.getSqlSession().getMapper(ClueDao.class);
        return clueDao.dispatcherDetail(clueId);
    }

    /**
     * 获取线索关联的市场活动列表
     *
     * @param clueId
     * @return
     */
    @Override
    public List<Activity> getActivityList(String clueId) {
        ClueActivityRelationDao relationDao = SqlSessionUtil.getSqlSession().getMapper(ClueActivityRelationDao.class);
        return relationDao.getActivityList(clueId);
    }

    /**
     * 解除关联
     *
     * @param relevanceId
     * @return
     */
    @Override
    public boolean clueActivityUnRelevance(String relevanceId) {
        ClueActivityRelationDao relationDao = SqlSessionUtil.getSqlSession().getMapper(ClueActivityRelationDao.class);
        return relationDao.clueActivityUnRelevance(relevanceId);
    }

    /**
     * 根据线索id查询关联的市场活动
     *
     * @param clueId
     * @return
     */
    @Override
    public List<Activity> getActivityListById(String clueId) {
        ClueActivityRelationDao relationDao = SqlSessionUtil.getSqlSession().getMapper(ClueActivityRelationDao.class);

        return relationDao.getActivityListById(clueId);
    }

}
